package com.reader.cms.config;

import com.reader.cms.interceptor.AuthorizationInterceptor;
import com.reader.cms.interceptor.MemberCheckInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 拦截器配置 add by jiahaikun
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Value("${ui.cors.allowedorigins}")
    private String allowedorigins;

    @Value("${ui.cors.pathpattern}")
    private String pathpattern;

    @Autowired
    private AuthorizationInterceptor authorizationInterceptor;

    @Autowired
    private MemberCheckInterceptor memberCheckInterceptor;


    /**
     * 允许跨域设置
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping(pathpattern).allowedOrigins(allowedorigins);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // UI API 拦截器
     //   registry.addInterceptor(authorizationInterceptor).addPathPatterns("/api/**");
        // 会员过期检查 拦截器
     //   registry.addInterceptor(memberCheckInterceptor).addPathPatterns("/api/**");

        // 开放openapi 拦截器
//        registry.addInterceptor(authorizationOpenAPIInterceptor).addPathPatterns("/openapi/**");
    }

}
